using System.Collections;
using System.Runtime.InteropServices;
using System;

namespace WindowsFormsApp1
{
    //SqlCipher lib Interface export c#
    public class SqliteDLL
    {
        /**
		 *	sqlite Result Codes
		 *		ex.https://www.sqlite.org/c3ref/c_abort.html
		 **/
        public const int SQLITE_OK = 0;
        public const int SQLITE_ERROR = 1;
        public const int SQLITE_ROW = 100;
        public const int SQLITE_DONE = 101;

        /**
		 *	sqlite data type
		 **/
        public const int SQLITE_INTEGER = 1;
        public const int SQLITE_FLOAT = 2;
        public const int SQLITE_TEXT = 3;
        public const int SQLITE_BLOB = 4;
        public const int SQLITE_NULL = 5;

#if UNITY_IPHONE
		const string SQLCIPHERDLL = "__Internal";
#elif UNITY_ANDROID
		const string SQLCIPHERDLL = "sqlcipher_android";
#else
        const string SQLCIPHERDLL = "sqlcipherbd";
#endif

        [DllImport(SQLCIPHERDLL, EntryPoint = "sqlite3_open", CallingConvention = CallingConvention.Cdecl)]
        public static extern int sqlite3_open(string filename, out IntPtr db);

        [DllImport(SQLCIPHERDLL, EntryPoint = "sqlite3_close", CallingConvention = CallingConvention.Cdecl)]
        public static extern int sqlite3_close(IntPtr db);

        [DllImport(SQLCIPHERDLL, EntryPoint = "sqlite3_key", CallingConvention = CallingConvention.Cdecl)]
        public static extern int sqlite3_key(IntPtr db, string key, int keyLen);

        [DllImport(SQLCIPHERDLL, EntryPoint = "sqlite3_prepare_v2", CallingConvention = CallingConvention.Cdecl)]
        public static extern int sqlite3_prepare_v2(IntPtr db, string zSql, int nByte, out IntPtr ppStmpt, IntPtr pzTail);

        [DllImport(SQLCIPHERDLL, EntryPoint = "sqlite3_step", CallingConvention = CallingConvention.Cdecl)]
        public static extern int sqlite3_step(IntPtr stmHandle);

        [DllImport(SQLCIPHERDLL, EntryPoint = "sqlite3_finalize", CallingConvention = CallingConvention.Cdecl)]
        public static extern int sqlite3_finalize(IntPtr stmHandle);

        [DllImport(SQLCIPHERDLL, EntryPoint = "sqlite3_errmsg", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr sqlite3_errmsg(IntPtr db);

        [DllImport(SQLCIPHERDLL, EntryPoint = "sqlite3_column_count", CallingConvention = CallingConvention.Cdecl)]
        public static extern int sqlite3_column_count(IntPtr stmHandle);

        [DllImport(SQLCIPHERDLL, EntryPoint = "sqlite3_column_name", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr sqlite3_column_name(IntPtr stmHandle, int iCol);

        [DllImport(SQLCIPHERDLL, EntryPoint = "sqlite3_column_type", CallingConvention = CallingConvention.Cdecl)]
        public static extern int sqlite3_column_type(IntPtr stmHandle, int iCol);

        [DllImport(SQLCIPHERDLL, EntryPoint = "sqlite3_column_int", CallingConvention = CallingConvention.Cdecl)]
        public static extern int sqlite3_column_int(IntPtr stmHandle, int iCol);

        [DllImport(SQLCIPHERDLL, EntryPoint = "sqlite3_column_text", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr sqlite3_column_text(IntPtr stmHandle, int iCol);

        [DllImport(SQLCIPHERDLL, EntryPoint = "sqlite3_column_double", CallingConvention = CallingConvention.Cdecl)]
        public static extern double sqlite3_column_double(IntPtr stmHandle, int iCol);

        [DllImport(SQLCIPHERDLL, EntryPoint = "sqlite3_column_blob", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr sqlite3_column_blob(IntPtr stmHandle, int iCol);

        [DllImport(SQLCIPHERDLL, EntryPoint = "sqlite3_column_bytes", CallingConvention = CallingConvention.Cdecl)]
        public static extern int sqlite3_column_bytes(IntPtr stmHandle, int iCol);

        [DllImport(SQLCIPHERDLL, EntryPoint = "sqlite3_bind_double", CallingConvention = CallingConvention.Cdecl)]
        public static extern int sqlite3_bind_double(IntPtr stmHandle, int idx, double value);

        [DllImport(SQLCIPHERDLL, EntryPoint = "sqlite3_bind_int", CallingConvention = CallingConvention.Cdecl)]
        public static extern int sqlite3_bind_int(IntPtr stmHandle, int idx, int value);

        [DllImport(SQLCIPHERDLL, EntryPoint = "sqlite3_bind_text", CallingConvention = CallingConvention.Cdecl)]
        public static extern int sqlite3_bind_text(IntPtr stmHandle, int idx, byte[] value, int length, IntPtr callback);

        [DllImport(SQLCIPHERDLL, EntryPoint = "sqlite3_bind_blob", CallingConvention = CallingConvention.Cdecl)]
        public static extern int sqlite3_bind_blob(IntPtr stmHandle, int idx, byte[] value, int valueLen, IntPtr callback);
    }
}